# 项目: PythonProject1
# 文件: test
# 作者: Zheng Hao
# 日期: 2025/5/20 18:00
# coding:utf-8


from selenium import webdriver
import time,random

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.select import Select


def get_driver():

    driver = webdriver.Edge()
    driver.implicitly_wait(5)   # 隐式等待5秒
    driver.maximize_window()    # 最大化窗口
    return driver

def input(element, value):
    element.click()     # 点击输入框
    element.clear()     # 清空原有内容
    element.send_keys(value)    # 输入值

def select_random_first(element):
    options_count = len(Select(element).options)    # 获取下拉选项总数
    random_index = random.randint(1, options_count-1)       # 随机索引（跳过第0项）
    Select(element).select_by_index(random_index)       # 按索引选择

if __name__ == '__main__':

    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/')
    uname = driver.find_element('name', 'userName')
    input(uname,'YD0006')       # 输入用户名
    upass = driver.find_element('name', 'userPass')
    input(upass, 'maxiaoliuYD0006')     # 输入密码
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()      # 点击登录
    time.sleep(3)


    driver.get('http://localhost:8080/YunDuanBoss/assets')
    driver.find_element('css selector', '#content > div.row.con-margin.con-body-con > div:nth-child(1) > div > button:nth-child(1)').click()        # 点击"添加资产"按钮


    assets_name = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(1) > div:nth-child(1) > select')
    select_random_first(assets_name)        # 随机选择设备名称


    assets_type = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(1) > div:nth-child(2) > span:nth-child(2) > select')
    select_random_first(assets_type)        # 随机选择设备型号



    barcode = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(2) > div:nth-child(1) > input')
    input(barcode, '98865532101')        # 输入合法的条码
    price = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(2) > div.col-md-6.col-sm-6.form-group > input')
    input(price, '800')     # 输入价格


    purchase_employee = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(3) > div:nth-child(1) > input.text.autoEmpId')
    input(purchase_employee, '马小六')
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.DOWN).perform()     # 按下箭头选择
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.ENTER).perform()        # 回车确认
    time.sleep(2)


    # driver.find_element('xpath', '//*[@id="addAss-form"]/div/div/div[3]/div[1]/ul/li/a').click()
    # //*[@id="time"]/input
    driver.find_element('css selector', '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > input:nth-child(2)').click()    # 打开日期控件
    time.sleep(1)
    driver.find_element('css selector', 'div.datetimepicker:nth-child(21) > div:nth-child(3) > table:nth-child(1) > tfoot:nth-child(3) > tr:nth-child(1) > th:nth-child(1)').click()    # 确认日期

    owner = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(4) > div:nth-child(2) > select')
    select_random_first(owner)      # 负责人随机选择
    time.sleep(2)


    driver.find_element('css selector', '#addAss > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > button:nth-child(1)').click()
    time.sleep(3)